<!DOCTYPE html>
<html>
  <head>
    <title>分配角色</title>
  </head>
  <body>
  	<div class="layui-card">
	  	<div class="layui-card-header">
	  		<p>您正在给${user.name}分配权限</p>
	  	</div>
	  	<div class="layui-card-body">
	  		<form class="layui-form">
	  			<ul id="roletree" class="ztree"></ul>
	  			<div class="layui-form-item" style="display: none">
				    <div class="layui-input-block">
				      <button class="layui-btn" id="submit-btn" lay-submit>立即提交</button>
				    </div>
				  </div>
	  		</form>
	  	</div>
	</div>	
  </body>
</html>
<script src="${ctx}/static/admin/layui/layui.js"></script>
<link rel="stylesheet" href="${ctx}/static/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${ctx}/static/jquery/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="${ctx}/static/ztree/jquery.ztree.all.min.js" charset="utf-8"></script>
<script>
var table;
layui.use(['jquery','form'], function(){
  var $ = jQuery = layui.$;
  var form = layui.form;
  
  $.fn.zTree.init($("#roletree"), setting, zNodes);
  zTree_Menu = $.fn.zTree.getZTreeObj("roletree");
  
  //监听提交
  form.on('submit', function(data){
	  var nodes = zTree_Menu.getCheckedNodes(true);
		var selectIds = "";
		for(var index in nodes){
			var item=nodes[index];
			if(item.id=='10000')continue;
			selectIds+=item.id+",";
		} 
		var submitData={
				roleIds : selectIds,
				id : '${user.id}'
		}
		$.post("${ctx}/sys_admin/sys/user/saveUserRoles" , submitData , 
			function(result){
				if (result.success) {
					table.reload({});
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index); //再执行关闭   
				} else {
					layer.msg(result.msg);
				}
		},"json");
	  return false;
  });
});

var zTree_Menu;
var zNodes = ${roletree.data};
var setting = {
	check: {  
      enable: true
  }, 
	callback : {
		beforeCheck: beforeCheck,
		onCheck: onCheck

	},
	data : {
		simpleData : {
			enable : true
		}
	}
}

function beforeCheck(treeId, treeNode) {
	return (treeNode.doCheck !== false);
}

function onCheck(e, treeId, treeNode) {

}
</script>